import ConfigParser
import os
from Constants import *


#####################################################################################################################################
# Config class
class Config:
	SECTION = 'AutoTag'

	FOLDER_INPUT = 'folderinput'
	FOLDER_OUPUT = 'folderoutput'
	COVER_NAME = 'covername'
	COVER_FILE_NAME = 'folder.jpg'
	FILE_OUTPUT='fileoutput'

	ARTIST_STR = '<ARTIST>'
	ALBUM_STR = '<ALBUM>'
	SOMETHING_STR = '<SOMETHING>'
	YEAR_STR = '<YEAR>'
	TRACKNUM_STR = '<TRACKNUM>'
	TRACKNAME_STR = '<TRACKNAME>'
	
	ARTIST = 0
	ALBUM = 1
	SOMETHING = 2
	YEAR = 3
	TRACKNUM = 4
	TRACKNAME = 5
	TAGS = (ARTIST_STR, ALBUM_STR, SOMETHING_STR, YEAR_STR, TRACKNUM_STR, TRACKNAME_STR)
	
	
	SEP = ' - '
	DEFAULT_FOLDER_IN = TAGS[ARTIST] + SEP + TAGS[SOMETHING] + SEP + TAGS[ALBUM]
	DEFAULT_FOLDER_OUT = TAGS[ARTIST] + SEP + TAGS[YEAR] + SEP + TAGS[ALBUM]
	DEFAULT_FILE_OUT = TAGS[TRACKNUM] + SEP + TAGS[TRACKNAME]
	FILENAME = os.environ['HOME'] + os.sep + '.autoTag'

	@staticmethod	
	def __read__(section, tag):
		if not os.path.exists(Config.FILENAME):
			Config.write_config()
		config = ConfigParser.ConfigParser()
		config.read(Config.FILENAME)
		return config.get(section,tag)

	@staticmethod
	def format_folder_input():
		return Config.__read__(Config.SECTION,Config.FOLDER_INPUT)
	
	@staticmethod
	def format_folder_output():
		return Config.__read__(Config.SECTION,Config.FOLDER_OUPUT)

	@staticmethod
	def format_file_output():
		return Config.__read__(Config.SECTION,Config.FILE_OUTPUT)
	
	@staticmethod	
	def write_config():
		fp = open(Config.FILENAME,'w')
		s = '''#AutoTag Config
#if you want change something, you are in the right place, enjoy!
#
#The options for the folders and files are:
#		<ARTIST> 	: The artist
#		<ALBUM>		: The album name
#		<YEAR>		: The year of the release
#		<TRACKNUM>	: The track number, only for files
#		<SOMETHING> : Other data in the folder name, must be year, comments, etc..
#
#you can combine this tags, the only mandatory tags are <ARTIST> and <ALBUM>
#
'''
		fp.write(s)
		config = ConfigParser.ConfigParser()
		config.add_section(Config.SECTION)
		config.set(Config.SECTION, Config.FOLDER_INPUT, Config.DEFAULT_FOLDER_IN)
		config.set(Config.SECTION, Config.FOLDER_OUPUT, Config.DEFAULT_FOLDER_OUT)
		config.set(Config.SECTION, Config.COVER_NAME, Config.COVER_FILE_NAME)
		config.set(Config.SECTION, Config.FILE_OUTPUT, Config.DEFAULT_FILE_OUT)
		config.write(fp)
		fp.close()
		
	@staticmethod
	def get_cover_name():
		return Config.COVER_FILE_NAME
